package com.testing.runner;

import com.testing.app.AppKeyword;
import com.testing.common.AutoTools;
import com.testing.common.ExcelReader;
import com.testing.common.ExcelWriter;
import com.testing.common.Report;
import com.testing.inter.InterKeyWord;
import com.testing.web.WebKeyword;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @Classname AutoFrame
 * @Description 类型说明
 * @Date 2021/6/30 22:36
 * @Created by 特斯汀Roy
 */
public class AutoFrame {

    private static String casefile = "Cases/";
    private static String resultfile = "Cases/Result/";
    private static Object keyWord;
    private static ExcelReader cases;
    private static ExcelWriter results;

    public static void main(String[] args) throws Exception {
        //将3个自动化类别整合到一起。
        String type = args[0];
        String caseName = args[1];
        String time = AutoTools.dateStr("MMdd-HHmmss");
        String startTime = AutoTools.dateStr("yyyy-MM-dd HH:mm:ss");
        switch (type) {
            case "web":
                casefile += caseName;
                resultfile += "WebResult" + time + ".xlsx";
                cases = new ExcelReader(casefile);
                results = new ExcelWriter(casefile, resultfile);
                keyWord = new WebKeyword();
                break;
            case "app":
                casefile += caseName;
                resultfile += "APPResult" + time + ".xlsx";
                cases = new ExcelReader(casefile);
                results = new ExcelWriter(casefile, resultfile);
                keyWord = new AppKeyword();
                break;
            case "inter":
                casefile += caseName;
                resultfile += "InterResult" + time + ".xlsx";
                cases = new ExcelReader(casefile);
                results = new ExcelWriter(casefile, resultfile);
                keyWord = new InterKeyWord();
                break;
        }
        //遍历每一行用例
        for (int sheetNo = 0; sheetNo < cases.getTotalSheetNo(); sheetNo++) {
            //指定使用当前的sheet页
            cases.useSheetByIndex(sheetNo);
            results.useSheetByIndex(sheetNo);
            for (int rowNo = 0; rowNo < cases.getRowNo(); rowNo++) {
                //设置关键字对象，当前要写入哪一行
                //执行用例，完成发包
                List<String> rowContent = cases.readLine(rowNo);
                if (rowContent.get(0) != null && rowContent.get(1) != null
                        && rowContent.get(0).trim().length() < 2 && rowContent.get(1).trim().length() < 2) {
                    //执行用例
                    boolean runRes = AutoTools.invokeKeyWord(keyWord, rowContent, 3);
                    if (rowContent.get(7).trim().length() > 2) {
                        //执行断言
                        runRes = AutoTools.invokeKeyWord(keyWord, rowContent, 7);
                    }
                    if (runRes) {
                        results.writeCell(rowNo, 10, "PASS");
                        if (keyWord instanceof InterKeyWord) {
                            results.writeCell(rowNo, 11, keyWord.getClass().getMethod("getResult").invoke(keyWord).toString());
                        }
                    } else {
                        results.writeFailCell(rowNo, 10, "FAIL");
                        if (keyWord instanceof InterKeyWord) {
                            results.writeFailCell(rowNo, 11, keyWord.getClass().getMethod("getResult").invoke(keyWord).toString());
                        }
                    }
                }
            }
        }
        cases.close();
        results.save();
        Report.sendreport(resultfile, startTime);

    }


}
